//
//  UserAccount.swift
//  WeiBo_Private
//
//  Created by Yangperlove on 16/1/13.
//  Copyright © 2016年 Yangperlove. All rights reserved.
//

import UIKit
import AFNetworking

class UserAccountViewModel: NSObject {
    //获取access_token
    func getAccess_Token(code: String, finished: (isSuccess: Bool) -> ()) {
        let urlStr = "https://api.weibo.com/oauth2/access_token"
        let params = ["client_id":client_id, "redirect_uri":redirect_uri, "grant_type":"authorization_code", "code":code, "client_secret":client_secret]
        
        NetworkTools.shareTools.networkRequest(.POST, urlString: urlStr, parameters: params) { (dict, error) -> () in
            if error != nil{
                finished(isSuccess: false)
                print(error)
                return
            }
            let userAccount = UserAccount(dict: dict!)
            //获取用户信息
            self.getUserInfo(userAccount,finished: finished)
        }
        
        
        //        //发送POST请求
        //        AFN.responseSerializer.acceptableContentTypes?.insert("text/plain")
        //
        //        AFN.POST(urlStr, parameters: params, progress: nil, success: { (_, result) -> Void in
        //            let dict = result as! [String:AnyObject]
        //            let userAccount = UserAccount(dict: dict)
        //            //获取用户信息
        //            self.getUserInfo(userAccount,finished: finished)
        //
        //            }) { (_, error) -> Void in
        //                print("error",error)
        //                finished(isSuccess: false)
        //        }
        
        
    }
    
    //获取用户信息
    func getUserInfo(userAccount: UserAccount, finished: (isSuccess: Bool) -> ()) {
        let urlStr = "https://api.weibo.com/2/users/show.json"
        let params = ["access_token":userAccount.access_token!, "uid":userAccount.uid!]
        
        NetworkTools.shareTools.networkRequest(.GET, urlString: urlStr, parameters: params) { (dict, error) -> () in
            if error != nil {
                finished(isSuccess: false)
                print(error)
                return
            }
            userAccount.name = dict!["name"] as? String
            userAccount.avatar_large = dict!["avatar_large"] as? String
            userAccount.saveUserAccount()
            finished(isSuccess: true)
            
        }
        
        //        print(params)
        //        AFN.GET(urlStr, parameters: params, progress: nil, success: { (_, result) -> Void in
        //            let dict = result as! [String:AnyObject]
        //            userAccount.name = dict["name"] as? String
        //            userAccount.avatar_large = dict["avatar_large"] as? String
        //            userAccount.saveUserAccount()
        //            finished(isSuccess: true)
        //            }) { (_, error) -> Void in
        //                print(error)
        //                finished(isSuccess: false)
        //        }
    }
    
    func isUserLogin() -> Bool {
        return UserAccount.getUserAccount() != nil
        
    }
    
    var userName: String? {
        return UserAccount.getUserAccount()?.name
    }
    
    var iconImage: String? {
        return UserAccount.getUserAccount()?.avatar_large
    }
    
    var access_token: String? {
        return UserAccount.getUserAccount()?.access_token
    }
    
}
